#include "service_client/thrift_client_pool.h"

#include "SearchEngine.h"

// using jztzp::SearchEngineClient;

void * ThreadFun(void * data) {
  ClientPool<SearchEngineClient> * pool = (ClientPool<SearchEngineClient>*)data;
  
  // const char * addrs[] = {"127.0.0.1:9091", "127.0.0.1:9093"};
  const char * addrs[] = {"127.0.0.1:10093"};
  int addr_count = sizeof(addrs) / sizeof(char*);

  SearchEngineClient * p = NULL;
  SearchResult res;

  // for (int i = 0; i < 100000; ++i) {
  for (int i = 0; i < 1; ++i) {
    std::string addr(addrs[i % addr_count]);
    p = pool->Alloc(addr);
    if (!p) {
      continue;
    }

    bool success = true;
    try {
      p->Search(res, "黄金", 0, 10);
    // p->Get(res, 3192);
      if (res.__isset.total_count) {
        std::cout << res.total_count << std::endl;
      } else {
        std::cout << "error" << std::endl;
      }
    } catch (const std::exception & e) {
      std::cout << addr << " error:" << e.what() << std::endl;
      success = false;
    }
    pool->Release(addr, p, success);
  }
  return NULL;
}

const int kThreadCount = 1;

int main() {
  LOG_INIT("./log", "WARN");
  ClientPool<SearchEngineClient> pool;

  pthread_t ts[kThreadCount];

  for (int i = 0; i < kThreadCount; ++i) {
    pthread_create(&ts[i], NULL, ThreadFun, &pool);
  }

  for (int i = 0; i < kThreadCount; ++i) {
    pthread_join(ts[i], NULL);
  }
  return 0;
}

